{!{- define "web_build_template" -}!}
{!{- $ctx := index . 0 -}!}
{!{- $docPart := index . 1 -}!}
{!{- $buildType := index . 2 -}!}

# <template: web_build_template>
runs-on: [self-hosted, regular]
steps:
  {!{ tmpl.Exec "started_at_output"         $ctx | strings.Indent 2 }!}
  {!{ tmpl.Exec "checkout_full_step"        $ctx | strings.Indent 2 }!}
  {!{ tmpl.Exec "login_dev_registry_step"   $ctx | strings.Indent 2 }!}
  {!{- $dir := "docs/documentation" -}!}
{!{ if eq $docPart "main" }!}
  {!{- $dir = "docs/site" -}!}
{!{ end }!}
{!{ if eq $buildType "release" }!}
  {!{ tmpl.Exec "login_rw_registry_step"    $ctx | strings.Indent 2 }!}
{!{ end }!}

  - name: Run {!{ $docPart }!} web build
    uses: {!{ index (ds "actions") "werf/actions/build" }!}
    with:
      channel: ${{env.WERF_CHANNEL}}
    env:
      WERF_DIR: "{!{ $dir }!}"
      WERF_LOG_VERBOSE: "on"
{!{- if eq $docPart "main" }!}
      DOC_API_KEY: "${{secrets.DOC_API_KEY}}"
      DOC_API_URL: "${{vars.DOC_API_URL}}"
{!{- end }!}
{!{- if eq $buildType "release" }!}
      WERF_REPO: "${{ steps.check_rw_registry.outputs.web_registry_path }}"
      WERF_SECONDARY_REPO: "${{ steps.check_dev_registry.outputs.web_registry_path }}"
{!{- else }!}
      WERF_REPO: "${{ steps.check_dev_registry.outputs.web_registry_path }}"
{!{- end }!}

# </template: web_build_template>
{!{- end -}!}

{!{- define "web_links_test_template" -}!}
# <template: web_links_test_template>
{!{- $ctx  := index . 0 }!}
{!{- $mode := index . 1 }!}
runs-on: [self-hosted, regular]
steps:
  {!{ tmpl.Exec "started_at_output"            $ctx | strings.Indent 2 }!}
  {!{ tmpl.Exec "checkout_full_step"           $ctx | strings.Indent 2 }!}
{!{ if eq $mode "release" }!}
  {!{ tmpl.Exec "login_rw_registry_step"       $ctx | strings.Indent 2 }!}
{!{- else }!}
  {!{ tmpl.Exec "login_dev_registry_step"      $ctx | strings.Indent 2 }!}
{!{- end }!}
  {!{ tmpl.Exec "werf_install_step"            $ctx | strings.Indent 2 }!}

  - name: Prepare site structure
    env:
      DOC_API_KEY: "${{secrets.DOC_API_KEY}}"
      DOC_API_URL: "${{vars.DOC_API_URL}}"
{!{ if eq $mode "release" }!}
      WEB_REGISTRY_PATH: ${{steps.check_rw_registry.outputs.web_registry_path}}
{!{- else }!}
      WEB_REGISTRY_PATH: ${{steps.check_dev_registry.outputs.web_registry_path}}
{!{- end }!}
    run: |
      type werf
      werf version

      BASEDIR=$(pwd)/docs
      _TMPDIR=$(mktemp -d -t -p ${BASEDIR})
      # Save TMPDIR to clean it later.
      echo "_TMPDIR=$_TMPDIR" >> ${GITHUB_ENV}
      echo "_TMPDIR=$_TMPDIR"

      export WERF_REPO="${WEB_REGISTRY_PATH}"
      echo -n 'use werf_repo '
      echo $WERF_REPO | tr 'a-z' 'A-Z'

      # Extract site content to the tmp directory.
      export WERF_DIR=$BASEDIR/site
      echo "Use werf_dir $WERF_DIR"
      type werf && source $(werf ci-env github --verbose --as-file)
      echo "werf stage image web-backend:"
      werf stage image web-backend | tr 'a-z' 'A-Z'
      echo "Run 'docker pull' from werf stage image web-backend"
      docker pull $(werf stage image web-backend) || true
      echo "werf stage image web-frontend:"
      werf stage image web-frontend | tr 'a-z' 'A-Z'
      echo "Run 'docker pull' from werf stage image web-frontend"
      docker pull $(werf stage image web-frontend) || true
      echo "Run 'docker cp' from werf stage image web-backend"
      docker cp $(docker create --rm $(werf stage image web-backend)):/app/root ${_TMPDIR}/backend
      echo "Run 'docker cp' from werf stage image web-frontend"
      docker cp $(docker create --rm $(werf stage image web-frontend)):/app ${_TMPDIR}/frontend

      # Extract documentation content to the tmp directory.
      export WERF_DIR=$BASEDIR/documentation
      type werf && source $(werf ci-env github --verbose --as-file)
      echo "werf stage image web:"
      werf stage image web | tr 'a-z' 'A-Z'
      echo "Run 'docker pull' from werf stage image web"
      docker pull $(werf stage image web)
      echo "Run 'docker cp' from werf stage image web"
      docker cp $(docker create --rm $(werf stage image web)):/app ${_TMPDIR}/documentation

      # Create EN site structure.
      echo "Create site structure in '${_TMPDIR}/site_en/'"
      mkdir -p ${_TMPDIR}/site_en/ ${_TMPDIR}/site_ru/
      touch ${_TMPDIR}/site_en/index.html ${_TMPDIR}/site_ru/index.html
      rsync -a --exclude='ru' --exclude='en' --exclude='compare' --exclude='includes/header.html' ${_TMPDIR}/frontend/ ${_TMPDIR}/site_en/
      rsync -a --exclude='ru' --exclude='en' --exclude='compare' --exclude='includes/header.html' ${_TMPDIR}/frontend/ ${_TMPDIR}/site_ru/
      #
      rsync -a ${_TMPDIR}/frontend/en/ ${_TMPDIR}/site_en/
      rsync -a ${_TMPDIR}/frontend/ru/ ${_TMPDIR}/site_ru/
      #
      rsync -a --exclude='includes/header.html' ${_TMPDIR}/backend/en/ ${_TMPDIR}/site_en/
      rsync -a --exclude='includes/header.html' ${_TMPDIR}/backend/ru/ ${_TMPDIR}/site_ru/
      #
      rsync -a --exclude='ru' --exclude='en' --exclude='compare' ${_TMPDIR}/documentation/ ${_TMPDIR}/site_en/documentation/
      rsync -a --exclude='ru' --exclude='en' --exclude='compare' ${_TMPDIR}/documentation/ ${_TMPDIR}/site_ru/documentation/
      rsync -a ${_TMPDIR}/documentation/en/ ${_TMPDIR}/site_en/documentation/
      rsync -a ${_TMPDIR}/documentation/ru/ ${_TMPDIR}/site_ru/documentation/
      #
      rsync -a ${_TMPDIR}/documentation/{assets,css,images,js} ${_TMPDIR}/site_en/documentation
      rsync -a ${_TMPDIR}/documentation/{assets,css,images,js} ${_TMPDIR}/site_ru/documentation

  - name: Check links with html-proofer (EN)
    run: |
      # Do not exit on html-proofer error.
      set +e
      # Checking EN site
      docker run --rm -v "${_TMPDIR}/site_en:/src:ro" klakegg/html-proofer:3.19.2 \
        --allow-hash-href --check-html --empty-alt-ignore \
        --url-ignore "/^\/(?!(gs\/|documentation\/|guides\/))/,/localhost/,/https\:\/\/t.me/,/docs-prv\.pcisecuritystandards\.org/,/gitlab.com\/profile/,/dash.cloudflare.com\/profile/,/example.com/,/vmware.com/,/.slack.com/,/habr.com/,/flant.ru/,/bcrypt-generator.com/,/candi\/bashible\/bashbooster/,/..\/..\/compare\//,/compare\/ru\//,/compare\/en\//,/\.yml$/,/\.yaml$/,/\.tmpl$/,/\.tpl$/" \
        --url-swap "https\:\/\/deckhouse.io\/:/,\/documentation\/v1\/:/documentation/,\/documentation\/latest\/:/documentation/" \
        --file_ignore "404.html" \
        --http-status-ignore "0,429" ${1}
      # Emulate 'allow_failure: true' from Gitlab. Github has only two state: success and failure.
      exit 0

  - name: Check links with html-proofer (RU)
    run: |
      # Do not exit on html-proofer error.
      set +e
      # Checking RU site
      docker run --rm -v "${_TMPDIR}/site_ru:/src:ro" klakegg/html-proofer:3.19.2 \
        --allow-hash-href --check-html --empty-alt-ignore \
        --url-ignore "/^\/(?!(gs\/|documentation\/|guides\/))/,/localhost/,/https\:\/\/t.me/,/docs-prv\.pcisecuritystandards\.org/,/gitlab.com\/profile/,/dash.cloudflare.com\/profile/,/example.com/,/vmware.com/,/.slack.com/,/habr.com/,/flant.ru/,/bcrypt-generator.com/,/candi\/bashible\/bashbooster/,/..\/..\/compare\//,/compare\/ru\//,/compare\/en\//,/\.yml$/,/\.yaml$/,/\.tmpl$/,/\.tpl$/" \
        --url-swap "https\:\/\/deckhouse.io\/:/,\/documentation\/v1\/:/documentation/,\/documentation\/latest\/:/documentation/" \
        --file_ignore "404.html" \
        --http-status-ignore "0,429" ${1}
      # Emulate 'allow_failure: true' from Gitlab. Github has only two state: success and failure.
      exit 0

  - name: Clean TMPDIR
    if: always()
    run: |
      if [[ -n $_TMPDIR ]] ; then
        rm -rf $_TMPDIR
      fi
# </template: web_links_test_template>
{!{- end -}!}

{!{/* doc version is a tag name or 'latest' for main branch and pull requests */}!}
{!{/* ci_commit_ref_name is a tagname or branch name */}!}
{!{ define "doc_version_template" }!}
# <template: doc_version_template>
- name: Set documentation version
  env:
    CI_COMMIT_TAG: ${{needs.git_info.outputs.ci_commit_tag}}
  run: |
    echo "DOC_VERSION=${CI_COMMIT_TAG:-latest}" >> $GITHUB_ENV
# </template: doc_version_template>
{!{- end -}!}

{!{ define "doc_release_version_template" }!}
{!{/* ci_commit_ref_name is a tagname or branch name */}!}
# <template: doc_release_version_template>
- name: Set documentation version
  env:
    CI_COMMIT_REF_NAME: ${{needs.git_info.outputs.ci_commit_ref_name}}
  run: |
    echo "DOC_VERSION=${CI_COMMIT_REF_NAME/release-/v}" >> $GITHUB_ENV
# </template: doc_release_version_template>
{!{- end -}!}

{!{ define "deploy_doc_template" }!}
{!{- $env := . -}!}
{!{- $webEnv := printf "web-%s" $env -}!}
{!{- $ns := printf "deckhouse-web-%s" $env -}!}
{!{- $url := printf "deckhouse.%s.flant.com" $env -}!}
{!{- $urlRu := printf "deckhouse.ru.%s.flant.com" $env -}!}
{!{- $kubeConfig := "${{ secrets.KUBECONFIG_BASE64_DEV }}" -}!}
{!{- $dcName := "dev" -}!}
{!{- $repo := "${{ steps.check_dev_registry.outputs.web_registry_path }}" -}!}
{!{- if or (eq $env "production") (eq $env "preproduction") -}!}
  {!{- $repo = "${{ steps.check_readonly_registry.outputs.web_registry_path }}" -}!}
  {!{- $url = "deckhouse.io" -}!}
  {!{- $urlRu = "deckhouse.ru" -}!}
  {!{- $kubeConfig = "${{ secrets.KUBECONFIG_BASE64_PROD }}" -}!}
  {!{- $dcName = "prod-eu" -}!}
{!{- end -}!}
{!{- if eq $env "preproduction" -}!}
  {!{- $ns = "deckhouse-web-production" -}!}
  {!{- $webEnv = "web-production" -}!}
  {!{- $kubeConfig = "${{ secrets.KUBECONFIG_BASE64_PROD_SEL }}" -}!}
  {!{- $dcName = "prod-sel" -}!}
{!{- end -}!}

# <template: deploy_doc_template>
- name: Deploy documentation to {!{ $env }!}
  uses: {!{ index (ds "actions") "werf/actions/converge" }!}
  with:
    channel: ${{env.WERF_CHANNEL}}
    kube-config-base64-data: "{!{ $kubeConfig }!}"
    env: {!{ $webEnv }!}
  env:
    WERF_REPO: {!{ $repo }!}
    WERF_DIR: "docs/documentation"
    WERF_RELEASE: "deckhouse-doc-${{ env.DOC_VERSION }}"
    WERF_NAMESPACE: {!{ $ns }!}
    WERF_SET_DOC_VERSION: "global.doc_version=${{ env.DOC_VERSION }}"
    WERF_SET_URL: "global.url={!{ $url }!}"
    WERF_SET_URL_RU: "global.url_ru={!{ $urlRu }!}"
    WERF_SET_WEB_ENV: "web.env={!{ $webEnv }!}"
    WERF_SET_DCNAME: "web.dc_name={!{ $dcName }!}"
# </template: deploy_doc_template>
{!{- end -}!}

{!{ define "deploy_site_template" }!}
{!{- $env := . -}!}
{!{- $webEnv := printf "web-%s" $env -}!}
{!{- $ns := printf "deckhouse-web-%s" $env -}!}
{!{- $url := printf "deckhouse.%s.flant.com" $env -}!}
{!{- $urlRu := printf "deckhouse.ru.%s.flant.com" $env -}!}
{!{- $kubeConfig := "${{ secrets.KUBECONFIG_BASE64_DEV }}" -}!}
{!{- $dcName := "dev" -}!}
{!{- $repo := "${{ steps.check_dev_registry.outputs.web_registry_path }}" -}!}
{!{- if or (eq $env "production") (eq $env "preproduction") -}!}
  {!{- $repo = "${{ steps.check_readonly_registry.outputs.web_registry_path }}" -}!}
  {!{- $url = "deckhouse.io" -}!}
  {!{- $urlRu = "deckhouse.ru" -}!}
  {!{- $kubeConfig = "${{ secrets.KUBECONFIG_BASE64_PROD }}" -}!}
  {!{- $dcName = "prod-eu" -}!}
{!{- end -}!}
{!{- if eq $env "preproduction" -}!}
  {!{- $ns = "deckhouse-web-production" -}!}
  {!{- $webEnv = "web-production" -}!}
  {!{- $kubeConfig = "${{ secrets.KUBECONFIG_BASE64_PROD_SEL }}" -}!}
  {!{- $dcName = "prod-sel" -}!}
{!{- end -}!}

{!{- $siteDomainMap := printf "{\"en\" : \"%s\", \"ru\" : \"%s\"}" $url $urlRu -}!}

# <template: deploy_site_template>
- name: Deploy site to {!{ $env }!}
  uses: {!{ index (ds "actions") "werf/actions/converge" }!}
  with:
    channel: ${{env.WERF_CHANNEL}}
    kube-config-base64-data: "{!{ $kubeConfig }!}"
    env: {!{ $webEnv }!}
  env:
    WERF_REPO: {!{ $repo }!}
    WERF_DIR: "docs/site"
    WERF_RELEASE: "deckhouse-site"
    WERF_NAMESPACE: {!{ $ns }!}
    WERF_SET_DOC_VERSION: "global.doc_version=${{ env.DOC_VERSION }}"
    WERF_SET_ACTIVE_RELEASE: "global.active_release=v1"
    WERF_SET_URL: "global.url={!{ $url }!}"
    WERF_SET_URL_RU: "global.url_ru={!{ $urlRu }!}"
    WERF_SET_WEB_ENV: "web.env={!{ $webEnv }!}"
    WERF_SET_DOMAIN_MAP: "global.domain_map={!{ base64.Encode $siteDomainMap }!}"
    WERF_SET_DCNAME: "web.dc_name={!{ $dcName }!}"
    DOC_API_KEY: "${{secrets.DOC_API_KEY}}"
    DOC_API_URL: "${{vars.DOC_API_URL}}"
    WERF_SET_MODULE_WATCHER_AUTHS: "global.moduleWatcherAuths=${{ secrets.MODULE_WATCHER_AUTHS }}"
# </template: deploy_site_template>
{!{- end -}!}
